package com.sopra.tecnica.poc.server.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;

import org.springframework.ui.jasperreports.JasperReportsUtils;
import org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView;

public class SaveFileJasperReportsPdfView extends JasperReportsPdfView {
	
	private Map<JRExporterParameter, Object> exporterParameters;
	
	@Override
	protected void onInit() {
		super.onInit();
		
		exporterParameters = new HashMap<JRExporterParameter, Object>();
		
		Map<JRExporterParameter, Object> previousExporterParameters = super.getConvertedExporterParameters();
		if (previousExporterParameters != null) {
			exporterParameters.putAll(previousExporterParameters);
		}
	}
	
	public void addExporterParameter(JRExporterParameter param, Object value) {
		exporterParameters.put(param, value);
	}

	@Override
	protected Map<JRExporterParameter, Object> getConvertedExporterParameters() {
		return exporterParameters;
	}
	
	@Override
	protected void renderReport(JasperPrint populatedReport, Map<String, Object> model,
			HttpServletResponse response) throws Exception {
		
		JRExporter exporter = createExporter();
		Map<JRExporterParameter, Object> mergedExporterParameters = getConvertedExporterParameters();
		exporter.setParameters(mergedExporterParameters);
		
		// IE workaround: write into byte array first.
		ByteArrayOutputStream baos = createTemporaryOutputStream();
		JasperReportsUtils.render(exporter, populatedReport, baos);
		writeToResponse(response, baos);
		
		// on recopie le pdf dans le fichier
		File outputFile = (File) model.get("outputFile");
		OutputStream out = new FileOutputStream(outputFile);
		baos.writeTo(out);
		out.close();
		
//		JavaMailSenderImpl sender = new JavaMailSenderImpl();
//		sender.setHost("smtp.gmail.com");
//		sender.setPort(465);
//		sender.setUsername("poctecnica@gmail.com");
//		sender.setPassword("sopra74poc");
//		Properties javaMailProperties = new Properties();
//		javaMailProperties.put("mail.smtp.auth", true);
//		javaMailProperties.put("mail.smtp.starttls.enable", true);
//		sender.setJavaMailProperties(javaMailProperties);
//		
//		MimeMessage message = sender.createMimeMessage();
//
//		// use the true flag to indicate you need a multipart message
//		MimeMessageHelper helper = new MimeMessageHelper(message, true);
//		helper.setTo("martin.soubeyrand@gmail.com");
//
//		helper.setText("Check out this image!");
//
//		// let's attach the infamous windows Sample file (this time copied to c:/)
//		FileSystemResource file = new FileSystemResource(outputFile);
//		helper.addAttachment("commande.pdf", file);
//
//		sender.send(message);
	}
}
